import { RouteRecordRaw, createWebHistory, createRouter } from "vue-router"

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        name: "firstPage",
        component: () => import("@/page/firstPage/index.vue"),
        meta: {
            title: "快点我"
        }
    },
    {
        path: "/home",
        name: "home",
        component: () => import("@/page/home/index.vue"),
        meta: {
            title: "move"
        }
    },
    {
        path: "/comment",
        name: "comment",
        component: () => import("@/page/comment/index.vue")
    },
    {
        path: "/:pathMatch(.*)*",
        name: "error",
        // component:()=>import("@/page/404/index.vue")
        redirect: "/"
    }
]
const router = createRouter({
    history: createWebHistory(import.meta.env.VITE_APP_ROUTER),
    // history:createWebHashHistory(),
    routes
})
router.beforeEach((to: any, _from, next) => {
    document.title = to.meta.title || ""
    //如果不是登录页面 并且没有token 就回到登陆页面 否则放行
    if (to.path != "/" && !localStorage.getItem("token")) {
        next({path:'/'})
    } else {
        next()
    }
})
export default router
